package zhoug.chart.charts;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.components.IMarker;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import zhoug.chart.core.MPCombinedKey;
import zhoug.chart.core.styleconfig.IMPCombinedStyleConfig;
import zhoug.chart.core.styleconfig.MPChartStyleDefaultUtils;
import zhoug.chart.core.styleconfig.MPCombinedStyleConfig;
import zhoug.chart.data.HkChartData;
import zhoug.chart.data.MPBarLineChartDataCreator;
import zhoug.chart.data.OnDateSetCreateListener;
import zhoug.chart.formatters.MPAxisTextValueFormatter;
import zhoug.chart.formatters.MPDataSetValueFormatter;
import zhoug.chart.ints.IMPChartPlaceholder;
import zhoug.chart.renderer.MPCombinedChartRenderer;

import java.util.List;

import androidx.annotation.NonNull;

/**
 * 混合图表
 *
 * @Author: zhoug
 * @Date: 2024-01-30
 * @Description:
 */
public class MPCombinedChart extends CombinedChart implements IMPChart<CombinedData, IMPCombinedStyleConfig> {
    protected MPChartHelper mChartHelper;
    protected IMPCombinedStyleConfig mStyleConfig;
    protected List<MPCombinedKey> mKeys;
    protected OnDateSetCreateListener<BarDataSet> mOnBarDateSetCreateListener;
    protected OnDateSetCreateListener<LineDataSet> mOnLineDateSetCreateListener;
    protected ValueFormatter mBarDataSetValueFormatter;
    protected ValueFormatter mLineDataSetValueFormatter;

    public MPCombinedChart(Context context) {
        this(context, null);
    }

    public MPCombinedChart(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MPCombinedChart(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mChartHelper = new MPChartHelper(this, context, attrs, defStyle);
        mStyleConfig = new MPCombinedStyleConfig();
        mBarDataSetValueFormatter=new MPDataSetValueFormatter();
        mLineDataSetValueFormatter=new MPDataSetValueFormatter();
        MPAxisTextValueFormatter axisTextValueFormatter = new MPAxisTextValueFormatter();
        XAxis xAxis = getXAxis();
        xAxis.setValueFormatter(axisTextValueFormatter);
        setAnimateDirection(ANIM_DIRECTION_X);

    }

    @Override
    protected void init() {
        super.init();
        mRenderer = new MPCombinedChartRenderer(this, mAnimator, mViewPortHandler);

    }

    /**
     * 柱子顶部是否绘制半圆
     *
     * @param barTopArc
     */
    public void setBarTopArcEnable(boolean barTopArc) {
        if(mRenderer instanceof MPCombinedChartRenderer mpCombinedChartRenderer){
            mpCombinedChartRenderer.setBarTopArcEnable(barTopArc);
        }
    }

    /**
     * 堆叠时只绘制顶部一个value
     *
     * @param barTopArc
     */
    public void setOnlyDrawValueStackedTop(boolean barTopArc) {
        if(mRenderer instanceof MPCombinedChartRenderer mpCombinedChartRenderer){
            mpCombinedChartRenderer.setOnlyDrawValueStackedTop(barTopArc);
        }
    }


    /**
     * 获取x轴数据格式化器
     *
     * @return
     */
    @Override
    public MPAxisTextValueFormatter getAxisTextValueFormatter() {
        return mChartHelper.getAxisTextValueFormatter();
    }


    @Override
    public void setData(CombinedData data) {
        if (data != null && getMarker() != null) {
            highlightValue(null);
        }
        super.setData(data);
    }

    @Override
    public void setData1(CombinedData data) {
        if (data != null) {
            MPChartStyleDefaultUtils.initXAxisMinMax(getXAxis(), data, mStyleConfig.isLineAttachAxis(),mStyleConfig.isOverlap());
            MPChartStyleDefaultUtils.initCombinedWidthAndScale(this, data, mStyleConfig);
            setData(data);
            notifyDataSetChanged();
            postInvalidate();
            mChartHelper.animate();
        } else {
            empty();
        }
    }

    /**
     * 设置数据
     *
     * @param list
     */
    @Override
    public void setHkChartData(List<HkChartData> list) {
        MPBarLineChartDataCreator creator = new MPBarLineChartDataCreator(mStyleConfig);
        creator.setBarDataSetValueFormatter(mBarDataSetValueFormatter);
        creator.setLineDataSetValueFormatter(mLineDataSetValueFormatter);
        creator.setOnBarDateSetCreateListener(mOnBarDateSetCreateListener);
        creator.setOnLineDateSetCreateListener(mOnLineDateSetCreateListener);
        CombinedData combinedData = creator.crateCombinedData(list, mKeys);
        MPAxisTextValueFormatter axisTextValueFormatter = getAxisTextValueFormatter();
        if (axisTextValueFormatter != null) {
            axisTextValueFormatter.setValueTexts(creator.getXLabels());
        }
        setData1(combinedData);
    }


    public void setKeys(List<MPCombinedKey> keys) {
        this.mKeys = keys;
    }

    public void setBarDataSetValueFormatter(ValueFormatter barDataSetValueFormatter) {
        this.mBarDataSetValueFormatter = barDataSetValueFormatter;
    }

    public void setLineDataSetValueFormatter(ValueFormatter lineDataSetValueFormatter) {
        this.mLineDataSetValueFormatter = lineDataSetValueFormatter;
    }

    public void setOnBarDateSetCreateListener(OnDateSetCreateListener<BarDataSet> onBarDateSetCreateListener) {
        this.mOnBarDateSetCreateListener = onBarDateSetCreateListener;
    }

    public void setOnLineDateSetCreateListener(OnDateSetCreateListener<LineDataSet> onLineDateSetCreateListener) {
        this.mOnLineDateSetCreateListener = onLineDateSetCreateListener;
    }

    public @NonNull
    IMPCombinedStyleConfig getStyleConfig() {
        return mStyleConfig;
    }

    public void setStyleConfig(@NonNull IMPCombinedStyleConfig styleConfig) {
        this.mStyleConfig = styleConfig;
    }

    /**
     * x轴开始的最小值
     *
     * @return
     */
    @Override
    public int getStartXValue() {
        return mChartHelper.getStartXValue();
    }

    @Override
    public void setChartPlaceholder(IMPChartPlaceholder chartPlaceholder) {
        mChartHelper.setChartPlaceholder(chartPlaceholder);
    }

    /**
     * 暂无数据
     */
    @Override
    public void empty() {
        mChartHelper.empty();
    }

    /**
     * 数据加载失败
     */
    @Override
    public void error() {
        mChartHelper.error();
    }

    /**
     * 重置占位图
     */
    @Override
    public void resetPlaceholder() {
        mChartHelper.resetPlaceholder();
    }

    /**
     * 占位图是否启用
     *
     * @param enable
     * @return
     */
    @Override
    public void setPlaceholderEnable(boolean enable) {
        mChartHelper.setPlaceholderEnable(enable);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        try {
            super.onDraw(canvas);
            mChartHelper.drawPlaceImage(canvas);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        mChartHelper.onLayout();
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mChartHelper.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

    /**
     * 不缩放,x,y的scale重置为1
     */
    @Override
    public void zoomNotScale() {
        mChartHelper.zoomNotScale();
    }

    /***
     * {@link #ANIM_DIRECTION_X,#ANIM_DIRECTION_Y,#ANIM_DIRECTION_XY}
     * @param animDirection
     */
    @Override
    public void setAnimateDirection(int animDirection) {
        mChartHelper.setAnimateDirection(animDirection);
    }

    /**
     * {@link #ANIM_DIRECTION_X,#ANIM_DIRECTION_Y,#ANIM_DIRECTION_XY}
     */
    @Override
    public int getAnimateDirection() {
        return mChartHelper.getAnimateDirection();
    }

    @Override
    public void setMarker(IMarker marker) {
        super.setMarker(marker);
        mChartHelper.setMarker(marker);
    }
}
